﻿using Microsoft.AspNetCore.Mvc;
using RabbitMQ.Client;
using RabbitMQ_Common;
using System.Text;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RabbitMQ_Proider.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FanoutSendController : ControllerBase
    {
        // GET: api/<FanoutSendController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            using (var connection = RabbitMQHelper.GetConnection())
            {
                using (var channel = connection.CreateModel())//创建信道，通信管道
                {
                    //声明交换机
                    channel.ExchangeDeclare("fanout_exchange", "fanout");

                    //创建队列
                    var queue = "fanout1";
                    channel.QueueDeclare(queue, false, false, false, null);
                    var queue2 = "fanout2";
                    channel.QueueDeclare(queue2, false, false, false, null);
                    var queue3 = "fanout3";
                    channel.QueueDeclare(queue3, false, false, false, null);
                    //绑定交换机
                    channel.QueueBind(queue, "fanout_exchange","");
                    channel.QueueBind(queue2, "fanout_exchange", "");
                    channel.QueueBind(queue3, "fanout_exchange", "");
                    //没绑定交换机，但由于rabbitmq有默认交换机
                    for (var i=0;i<10;i++)
                    {
                        string message = "测试1"+i;
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("fanout_exchange", "", null, body);
                    }
                    

                }
            }
            return new string[] { "ok" };
        }

       
    }
}
